

class ResourceController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ resourceInstanceList: Resource.list( params ), resourceInstanceTotal: Resource.count() ]
    }

    def show = {
        def resourceInstance = Resource.get( params.id )

        if(!resourceInstance) {
            flash.message = "Resource not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ resourceInstance : resourceInstance ] }
    }

    def delete = {
        def resourceInstance = Resource.get( params.id )
        if(resourceInstance) {
            try {
                resourceInstance.delete(flush:true)
                flash.message = "Resource ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Resource ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Resource not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def resourceInstance = Resource.get( params.id )

        if(!resourceInstance) {
            flash.message = "Resource not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ resourceInstance : resourceInstance ]
        }
    }

    def update = {
        def resourceInstance = Resource.get( params.id )
        if(resourceInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(resourceInstance.version > version) {
                    
                    resourceInstance.errors.rejectValue("version", "resource.optimistic.locking.failure", "Another user has updated this Resource while you were editing.")
                    render(view:'edit',model:[resourceInstance:resourceInstance])
                    return
                }
            }
            resourceInstance.properties = params
            if(!resourceInstance.hasErrors() && resourceInstance.save()) {
                flash.message = "Resource ${params.id} updated"
                redirect(action:show,id:resourceInstance.id)
            }
            else {
                render(view:'edit',model:[resourceInstance:resourceInstance])
            }
        }
        else {
            flash.message = "Resource not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def resourceInstance = new Resource()
        resourceInstance.properties = params
        return ['resourceInstance':resourceInstance]
    }

    def save = {
        def resourceInstance = new Resource(params)
        if(!resourceInstance.hasErrors() && resourceInstance.save()) {
            flash.message = "Resource ${resourceInstance.id} created"
            redirect(action:show,id:resourceInstance.id)
        }
        else {
            render(view:'create',model:[resourceInstance:resourceInstance])
        }
    }
}
